/*
 * ChooseNormalInvoicePresenter.java
 * business
 *
 * Created by ChenTao on 2017/1/7.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.shoppingcart.chooseinvoice.normal;

import android.text.TextUtils;

import com.yidiandao.business.R;
import com.yidiandao.business.data.model.IInvoiceModel;
import com.yidiandao.business.data.model.InvoiceModel;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.Functional;
import com.yidiandao.business.ui.widget.listview.displaylistitem.ChooseNormalInvoiceListItem;
import com.yidiandao.business.utils.StringUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择普通发票Presenter
 */
public class ChooseNormalInvoicePresenter implements ChooseNormalInvoiceContract.Presenter {

    private ChooseNormalInvoiceContract.View mNormalInvoiceView;

    private IInvoiceModel mInvoiceModel;

    private String mDefaultNormalInvoice;

    private List<String> mNormalInvoiceList;

    private List<String> mFilterInvoiceList = new ArrayList<>();

    private List<DisplayListItem> mClearListItems = new ArrayList<>();

    private String mInvType;

    public ChooseNormalInvoicePresenter(ChooseNormalInvoiceContract.View normalInvoiceView) {
        mNormalInvoiceView = normalInvoiceView;
        mNormalInvoiceView.setPresenter(this);

        mInvoiceModel = new InvoiceModel();
    }

    @Override
    public void start() {
    }

    public void start(String invType, String defaultInvoice, List<String> invoiceList) {
        mInvType = invType;
        mDefaultNormalInvoice = defaultInvoice;
        mNormalInvoiceList = invoiceList;
        reShowNormalInvoice();
    }

    private void reShowNormalInvoice() {
        if (!TextUtils.isEmpty(mDefaultNormalInvoice)) {
            // 回显普通发票
            mNormalInvoiceView.refreshTitle(mDefaultNormalInvoice, true);
        }
    }

    @Override
    public void filterNormalInvoiceList(String text) {
        // 手动关键字搜索
        mFilterInvoiceList.clear();
        for (String content : mNormalInvoiceList) {
            if (content.contains(text) && !mFilterInvoiceList.contains(text)) {
                mFilterInvoiceList.add(content);
            }
        }

        mNormalInvoiceView.setInvoiceListItems(generateListItems(mFilterInvoiceList));
    }

    @Override
    public void onItemClick(int position) {
        String title = mFilterInvoiceList.get(position);
        mNormalInvoiceView.refreshTitle(title, false);
        mNormalInvoiceView.setInvoiceListItems(mClearListItems);
    }

    private List<DisplayListItem> generateListItems(List<String> normalInvoiceList) {
        return Functional.serverToClient(
                normalInvoiceList,
                new Functional.Processor<String, DisplayListItem>() {
                    @Override
                    public DisplayListItem processToClient(String s) {
                        return new ChooseNormalInvoiceListItem(s);
                    }
                });
    }

    @Override
    public void onConfirmNormalInvoice() {
        // 先检查当前的发票是否在已存在的发票列表中
        String inputTitle = mNormalInvoiceView.getInputTitle();

        if (TextUtils.isEmpty(inputTitle)) {
            mNormalInvoiceView.showToast(UIUtils.getString(R.string.invoice_header_can_not_be_null));
            return;
        }

        // 需要发票时发票抬头不能包含Emoji表情
        if (!TextUtils.isEmpty(inputTitle) && StringUtils.containsEmoji(inputTitle)) {
            mNormalInvoiceView.showToast(UIUtils.getString(R.string.inv_cant_input_emoji));
            return;
        }

        if (!mNormalInvoiceList.contains(inputTitle)) {
            // 如果不存在，则先添加为新发票
            addNormalInvoice(inputTitle);
        } else {
            changeNormalInvoice(inputTitle);
        }
    }

    @Override
    public void changeNormalInvoice(String invoiceTitle) {

        mInvoiceModel.changeInvoice(mNormalInvoiceView.getPagerTag(), mInvType, invoiceTitle, new IInvoiceModel.OnNetAccessListener() {
            @Override
            public void onSuccess() {
                mNormalInvoiceView.finishActivity();
            }

            @Override
            public void onFailed() {

            }
        });

    }

    @Override
    public void addNormalInvoice(final String inputTitle) {
        mInvoiceModel.addNormalInvoice(inputTitle, mNormalInvoiceView.getPagerTag(), new IInvoiceModel.OnNetAccessListener() {
            @Override
            public void onSuccess() {
                changeNormalInvoice(inputTitle);
            }

            @Override
            public void onFailed() {
            }
        });
    }
}
